//
// Copyright (c) 2014, 保留所有权利。
//
// AvoidDisturb.java
//
// 作    者：王红燕
// 当前版本：1.0
// 2014 上午10:55:35
//
// 
//
package com.vanke.agent.common;

import java.util.Calendar;


import org.apache.log4j.Logger;

/**
 *
 */
public class AvoidDisturb {
	static Logger log = Logger.getLogger(AvoidDisturb.class);

	static class HM {
		long hour;
		long min;
	}

	static HM begin = new HM();
	static HM end = new HM();

	public static void setBegin(int hour, int min) {
		begin.hour = hour;
		begin.min = min;
	}

	public static void setEnd(int hour, int min) {
		end.hour = hour;
		end.min = min;
	}

	public static void checkAndWait(long second) {
		long count = 0;
		while (true) {
			Calendar cal = Calendar.getInstance();
			int hour = cal.get(Calendar.HOUR_OF_DAY);
			int min = cal.get(Calendar.MINUTE);
			long now = hour * 3600 + min * 60;

			long realBegin = begin.hour * 3600 + begin.min * 60 - second;
			long realEnd = end.hour * 3600 + end.min * 60;

			if (now >= realBegin && now <= realEnd) {
				break;
			}
			try {
				if (count++ % 100 == 0) {
					count = 1;
					log.info("wait for, begin:" + realBegin + ", now: " + now
							+ ", end:" + realEnd);
				}
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} // end while(true)
		log.info("check and wait, OK, go on!");
	}

	public static void main(String[] args) throws Exception {
		while (true) {
			setBegin(15, 37);
			setEnd(15, 38);
			checkAndWait(0);
			Thread.sleep(1000);
		}
	}
}
